package ecg.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.opensymphony.xwork2.ActionSupport;

public class MailManager extends ActionSupport {
	// 첨부될 실제 파일명이 저장된 리스트.
		 List<String> realFileNameList = new ArrayList<String>();
		 
		private static MailManager singleton;
			
		private MailManager(){}
		
		/**
		 * 싱글톤 객체 생성
		 * 
		 * @return 없음
		 */
		public static MailManager getInstance()
		{
			if(singleton == null) {
				singleton = new MailManager();
			}
			
			return singleton;
		}
		
		public void doMailSendSimple(Map<String, String> mailDataMap) throws Exception
		{
			String receiver 		= mailDataMap.get("receiver");
			String receiverEmail 	= mailDataMap.get("receiverEmail");
			String sender 			= mailDataMap.get("sender");
			String senderEmail 		= mailDataMap.get("senderEmail");
			String subject  		= mailDataMap.get("subject");
			String contentsGubun 	= mailDataMap.get("contentsGubun");
			String content 			= mailDataMap.get("content");
			String memNo			= mailDataMap.get("memNo");
			String memName			= mailDataMap.get("memName");
			String writeTime		= mailDataMap.get("writeTime");
			
			Map<String, String> mailMap = null;
			
			// 메일을 보낼 SMTP서버와 프로그램이름 설정
			/*String mailhost 		= "127.0.0.1";*/
			String mailhost 		= "1.234.80.177";
			String mailport 		= "25";
			String mailer   		= "JSP Mail";
			
			String contents = this.makeMailContents(mailDataMap, contentsGubun);	// 메일 컨텐츠 생성.
			
			try{
			  Properties props = System.getProperties();
			  
		      props.put("mail.smtp.host", mailhost);
		      props.put("mail.smtp.port", mailport);
		      
		      Session session = Session.getDefaultInstance(props, null);

		      // 메일을 보낼 Message클래스를 생성한다.
		      Message msg = new MimeMessage(session);

		      // 메일 구성요소 설정
		      msg.setFrom(new InternetAddress(mailDataMap.get("senderEmail"),mailDataMap.get("sender"),"euc-kr")); // 발신인의 이름을 한글로 표시할 수 있다.
		      msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receiverEmail, false));
		      msg.setSubject(subject);
		      msg.setContent(contents, "text/html; charset=utf-8");
		      msg.setSentDate(new Date());
		      msg.setText(contents);
		      msg.setHeader("Content-Type", "text/html; charset=utf-8");
		      msg.setHeader("X-Mailer", mailer);

		      // 메일 전송
		      Transport.send(msg);	      
			}catch(MessagingException e) {
				System.out.println("이메일 발송 에러입니다. : " + e);
			}catch(Exception ex) {
				System.out.println("에러");
			}
		}
		
		private String makeMailContents(Map<String,String> mailData, String contentsGubun)
		{
			String mailContents = "";
			
			if(contentsGubun.equals("contents")) {
				mailContents = this.makeMailContentsTempPasswd(mailData);
			}
			
			return mailContents;
		}
		
		private String makeMailContentsTempPasswd(Map<String,String> mailData)
		{
			StringBuilder builder = new StringBuilder();
			
			builder.append("<div>")
					.append("<span>").append(mailData.get("receiver")).append("님,<br/>")
					.append(mailData.get("memName")).append("(사번:").append(mailData.get("memNo")).append(")님이 제보한 정보입니다. <br/><br/>")
					.append(mailData.get("content"))
					.append("<br/> 등록일 : ").append(mailData.get("writeTime")).append("<br/>")
					.append("</div>");
					
			return builder.toString();
		}
}
